#include <iostream>
#include <fstream>
#include <log4cpp/Category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Priority.hh>
using namespace std;
using namespace log4cpp;

void test0(){
    // 1. 准备布局（格式化器）
    PatternLayout * ptnConsole = new PatternLayout();
    ptnConsole->setConversionPattern("%d %c: [%p] %m%n");

    PatternLayout * ptnFile = new PatternLayout();
    ptnFile->setConversionPattern("%d %c: [%p] %m%n");

    PatternLayout * ptnRollingFile = new PatternLayout();
    ptnRollingFile->setConversionPattern("%d %c: [%p] %m%n");

    // 2. 创建输出器与布局绑定 输出器和布局要一对一
    OstreamAppender * pConsole = new OstreamAppender("console", &cout);
    FileAppender * pFile = new FileAppender("file", "res.log");
    RollingFileAppender * pRollingFile = new RollingFileAppender("rollingFile", "rolling.log", 5 * 1024, 9);

    pConsole->setLayout(ptnConsole);
    pFile->setLayout(ptnFile);
    pRollingFile->setLayout(ptnRollingFile);

    // 3.创建记录器
    Category& log = Category::getInstance("log");

    // 4.设置优先级（过滤器）和输出器 
    log.setPriority(Priority::DEBUG);
    log.setAppender(pConsole);
    log.setAppender(pFile);
    log.setAppender(pRollingFile);

    // 5. 利用记录器写日志 
    int count = 30;
    while(count--){
        log.emerg("This is an emergce msg");
        log.fatal("This is a fatal msg");
        log.alert("This is an alert msg");
        log.crit("This is a crit msg");
        log.error("This is an error msg");
        log.warn("This is a warn msg");
        log.notice("This is a notice msg");
        log.info("This is an info msg");
        log.debug("This is a debug msg");
    }

    // 回收log4cpp用到的资源（以防万一）不要自己delete
    Category::shutdown();
}

int main(void){
    test0();
    return 0;
}